﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Agehack.Icq;

namespace DemoProject
{
    public partial class frmMain : Form
    {
        private bool logSet = false;
        CanonOscar.IcqClient icq = new CanonOscar.IcqClient();
        public frmMain()
        {
            InitializeComponent();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            icq.Disconnect();
            if (logSet)
                Agehack.TrafficLogger.LogStream.Close();
            Application.Exit();
        }

        #region Connection
        private void tmrUpdateInfo_Tick(object sender, EventArgs e)
        {
            if (icq.IsConnected)
            {
                lblStatus.Text = "Connected";
                grbConnect.Enabled = false;
                btnDisconnect.Enabled = false;
            }
            else
            {
                lblStatus.Text = "Disconnected";
                grbConnect.Enabled = true;
                btnDisconnect.Enabled = true;
            }
        }
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (!icq.IsConnected)
            {
                if (bgwConnect.IsBusy)
                    return;

                setLog();
                bgwConnect.RunWorkerAsync(icq);
            }
            else 
                MessageBox.Show("You are already connected.");
        }
        private void bgwConnect_DoWork(object sender, DoWorkEventArgs e)
        {
            CanonOscar.IcqClient.AuthRequest auth = new CanonOscar.IcqClient.AuthRequest(
                "login.icq.com", txtUIN.Text, txtPassword.Text);
            auth.AuthorizationProgressChanged += new CanonOscar.IcqClient.AuthRequest.ClientAuthorizationProgressChanged(auth_AuthorizationProgressChanged);
            icq.Connection.OnPacketReceived += new Agehack.FlapConnection.PacketEvent(Connection_OnPacketReceived);
            icq.Connection.OnPacketSent += new Agehack.FlapConnection.PacketEvent(Connection_OnPacketSent);
            icq.Connection.RaiseEvents = true;
            icq.Connect(ref auth);
        }
        void auth_AuthorizationProgressChanged(CanonOscar.IcqClient.AuthRequest clientUser, CanonOscar.IcqClient.AuthRequest.AuthStage status)
        {
            Application.OpenForms[0].
                Invoke(
                    new authProgressChangedH(authProgressChanged), 
                    new object[] { clientUser });
        }
        public delegate void authProgressChangedH(CanonOscar.IcqClient.AuthRequest c);
        public void authProgressChanged(CanonOscar.IcqClient.AuthRequest c)
        {
            switch (c.Progress)
            {
                case CanonOscar.IcqClient.AuthRequest.AuthStage.HostnameLookup:
                    pgbAuthProgress.Value = 10;
                    lblIcqStatus.Text = "Looking up server...";
                    break;
                case CanonOscar.IcqClient.AuthRequest.AuthStage.Connecting:
                    pgbAuthProgress.Value = 20;
                    lblIcqStatus.Text = "Connecting...";
                    break;
                case CanonOscar.IcqClient.AuthRequest.AuthStage.Connected:
                    pgbAuthProgress.Value = 30;
                    lblIcqStatus.Text = "Connected...";
                    break;
                case CanonOscar.IcqClient.AuthRequest.AuthStage.Authorization:
                    pgbAuthProgress.Value = 40;
                    lblIcqStatus.Text = "Authorization...";
                    break;
                case CanonOscar.IcqClient.AuthRequest.AuthStage.Reconnecting:
                    pgbAuthProgress.Value = 70;
                    lblIcqStatus.Text = "Reconnecting to BOSS...";
                    break;
                case CanonOscar.IcqClient.AuthRequest.AuthStage.Negotiating:
                    pgbAuthProgress.Value = 80;
                    lblIcqStatus.Text = "Negotiating...";
                    break;
                case CanonOscar.IcqClient.AuthRequest.AuthStage.ConnectedAuthorizedRead:
                    pgbAuthProgress.Value = 99;
                    lblIcqStatus.Text = "Connection ok, session installed.";
                    break;
                default:
                    pgbAuthProgress.Value = 0;
                    icq.Disconnect();
                    if (c.Error != null)
                        MessageBox.Show("Error: " + c.Error.Message);
                    lblIcqStatus.Text = "Unknown";
                    break;
            }
        }
        public void authDone()
        {
            icq.Connection.OnDisconnected +=
                new Agehack.FlapConnection.ConnectionDisconnected(Connection_OnDisconnected);
            lblIcqStatus.Text = "Online in ICQ";
        }
        void Connection_OnDisconnected(Agehack.FlapConnection con)
        {
            throw new NotImplementedException(); //fixme
        }
        void Connection_OnPacketReceived(Agehack.FlapConnection con, Agehack.Flap packet)
        {
            Agehack.TrafficLogger.Log(true, packet);
        }
        void Connection_OnPacketSent(Agehack.FlapConnection con, Agehack.Flap packet)
        {
            Agehack.TrafficLogger.Log(false, packet);
        }
        public void setLog()
        {
            if (logSet)
                Agehack.TrafficLogger.LogStream.Dispose();
            Agehack.TrafficLogger.LogStream =
                new System.IO.FileStream("traffic_in.dump",
                    System.IO.FileMode.Create, System.IO.FileAccess.Write);
            logSet = true;
        }
        private void bgwConnect_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lblIcqStatus.Text = "Online";
        }
        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            if (icq.IsConnected)
                icq.Disconnect();
        }
        #endregion
    }
}
